main or master.Git commands follow a standard structure, which helps in understanding how to use them.
git <command> [<subcommand>] [-options] [<arguments>]
<command>: The main action you want to perform (e.g., commit, branch, push).[<subcommand>]: An optional secondary action (e.g., git remote add).[-options]: Modifiers for the command, also called flags (e.g., git commit -m "message"). Short form is one dash (-m), long form is two dashes (--message).[<arguments>]: The target of the command, often a file path or a branch name (e.g., git add index.html).git config --global user.name "[name]" and git config --global user.email "[email]"git initgit clone [url]git statusgit add [file]git add .git diffgit diff --stagedgit commit -m "[descriptive message]"git restore --staged [file]git restore [file]git branchgit branch [branch-name]git checkout [branch-name]git checkout -b [branch-name]git merge [branch-name]git rebase [base-branch-name]git branch -d [branch-name] (-D to force)git remote -vgit remote add [name] [url] (e.g., origin)git fetch [remote-name]git pull [remote-name] [branch-name]git push [remote-name] [branch-name]git push -u [remote-name] [branch-name]main branch:
git checkout main
git pull origin main
git checkout -b new-feature-branch
git add .
git commit -m "Add new feature X"
git push -u origin new-feature-branch
git checkout main
git branch -d new-feature-branch
main branch. This keeps the main line of development clean and stable.
Create shortcuts for long commands in your .gitconfig file.
git config --global alias.co checkout (Now use git co)git config --global alias.br branch (Now use git br)git config --global alias.ci commit (Now use git ci)git config --global alias.st status (Now use git st)git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git commit -am "message" adds all tracked files and commits.git checkout -git add -p lets you review and stage parts of a file.
Working Directory ---> Staging Area ---> Local Repository
(Untracked files) | (Index) | (.git directory)
| |
(modified file) --git add--> (staged file) --git commit--> (versioned file)
| Aspect | git merge |
git rebase |
|---|---|---|
| History | Preserves history as it happened. Creates a "merge commit". | Rewrites history to create a linear, cleaner timeline. |
| Collaboration | Safer for public/shared branches as it doesn't change existing commits. | Can be dangerous on shared branches. Best used on private feature branches. |
| Complexity | Simpler to use and understand. | More powerful, but can be more complex to resolve conflicts. |
You don't need a new commit. You can amend the previous one.
git add [forgotten-file]git commit --amend --no-edit--no-edit flag keeps the original commit message. Omit it to write a new one. Be careful amending commits that have already been pushed.git remote add upstream [original-repo-url]git fetch upstreamgit checkout maingit merge upstream/maingit reset --soft HEAD~1git reset HEAD~1git reset --hard HEAD~1 (Use with caution!)git checkout correct-branchgit cherry-pick [commit-hash-from-wrong-branch]git checkout wrong-branchgit reset --hard HEAD~1git reflog shows a log of all actions where HEAD was changed. Find the commit hash in the reflog and you can check it out or cherry-pick it back.git reset --hard and git rebase) on branches that are shared with other people.